package com.aracky.assess.service.impl;

import com.aracky.assess.mapper.HrKpiUserAssessorConfigMapper;
import com.aracky.assess.model.entity.HrKpiUserAssessorConfig;
import com.aracky.assess.model.request.HrKpiUserAssessorConfigRequest;
import com.aracky.assess.model.request.UserAndAssessorConfigDetailRequest;
import com.aracky.assess.model.vo.HrKpiUserAssessorConfigPageListVo;
import com.aracky.assess.model.vo.HrKpiUserAssessorConfigVO;
import com.aracky.assess.model.vo.UserAndAssessorConfigDetailVo;
import com.aracky.assess.service.HrKpiUserAssessorConfigService;
import com.aracky.common.util.UserIPUtil;
import com.aracky.common.vo.PageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 考核关系配置表 服务实现类
 * </p>
 *
 * @author zzp
 * @since 2022-08-01 09:43:18
 */
@Service
public class HrKpiUserAssessorConfigServiceImpl extends ServiceImpl<HrKpiUserAssessorConfigMapper, HrKpiUserAssessorConfig> implements HrKpiUserAssessorConfigService {
    @Resource
    private HrKpiUserAssessorConfigMapper hrKpiUserAssessorConfigMapper;

    /**
     * 查询列表
     * @param hrKpiUserAssessorConfigRequest
     * @return
     */
    @Override
    public PageVO<HrKpiUserAssessorConfigVO> getHrKpiUserAssessorConfigList(HrKpiUserAssessorConfigRequest hrKpiUserAssessorConfigRequest) {
        IPage<HrKpiUserAssessorConfigVO> page=hrKpiUserAssessorConfigMapper.findHrKpiUserAssessorConfig(hrKpiUserAssessorConfigRequest.toPage(),hrKpiUserAssessorConfigRequest);
        return PageVO.convertPage(page);
    }

    /**
     * 分页查询出员工考核关系列表信息
     *
     * @param hrKpiUserAssessorConfigRequest
     * @return
     */
    @Override
    public PageVO<HrKpiUserAssessorConfigPageListVo> getPageList(HrKpiUserAssessorConfigRequest hrKpiUserAssessorConfigRequest) {
        if (null == hrKpiUserAssessorConfigRequest) {
            return null;
        }
        IPage<HrKpiUserAssessorConfigPageListVo> page = hrKpiUserAssessorConfigMapper.selectPageList(hrKpiUserAssessorConfigRequest.toPage(),hrKpiUserAssessorConfigRequest);
        return PageVO.convertPage(page);
    }

    /**
     * 根据id，查询员工考核关系信息
     *
     * @param id
     * @return
     */
    @Override
    public HrKpiUserAssessorConfigPageListVo getMyById(Long id) {
        HrKpiUserAssessorConfigPageListVo hrKpiUserAssessorConfigPageListVo= hrKpiUserAssessorConfigMapper.selectMyById(id);
        return hrKpiUserAssessorConfigPageListVo;
    }

    /**
     * 分页查询员工和考核关系信息详情
     *
     * @param userAndAssessorConfigDetailRequest
     * @return
     */
    @Override
    public PageVO<UserAndAssessorConfigDetailVo> getPageUserAndAssessorConfigDetail(UserAndAssessorConfigDetailRequest userAndAssessorConfigDetailRequest) {
        IPage<UserAndAssessorConfigDetailVo> page = hrKpiUserAssessorConfigMapper.selectPageUserAndAssessorConfigDetail(userAndAssessorConfigDetailRequest.toPage());
        if (null == page) {
            return null;
        }
        return PageVO.convertPage(page);
    }

    /**
     * 根据员工id查询出员工考核关系信息
     *
     * @param userId
     * @return
     */
    @Override
    public HrKpiUserAssessorConfigPageListVo getByUserId(Long userId) {
        if (null == userId){
            return null;
        }
        HrKpiUserAssessorConfigPageListVo hrKpiUserAssessorConfigPageListVo = hrKpiUserAssessorConfigMapper.selectByUserId(userId);
        return hrKpiUserAssessorConfigPageListVo;
    }

    /**
     * 通过id进行修改考核关系(update需要兼容某些字段置空--先删后新增)
     *
     * @param hrKpiUserAssessorConfig
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void myUpdateById(HrKpiUserAssessorConfig hrKpiUserAssessorConfig) throws Exception {
        hrKpiUserAssessorConfig.setUpdateBy(UserIPUtil.getCurrentUserName());
        hrKpiUserAssessorConfig.setUpdateTime(LocalDateTime.now());
        Integer update = hrKpiUserAssessorConfigMapper.myUpdateById(hrKpiUserAssessorConfig);
        // int delete = hrKpiUserAssessorConfigMapper.deleteById(hrKpiUserAssessorConfig.getId());
        // hrKpiUserAssessorConfig.setId(null);
        // int insert = hrKpiUserAssessorConfigMapper.insert(hrKpiUserAssessorConfig);
        // if (delete < 0 || insert < 0) {
        //     throw new Exception("修改失败！");
        // }
    }
}
